/**@file   usart.c 
 * @brief  串口操作的实现
 * 
 * @author SealedGhost
 */
 
 
#include "usart.h" 
#include "misc.h"
#include "stm32f10x_gpio.h"

#include "bsp_private.h"

#include <stdio.h>


#if 1
#pragma import (__use_no_semihosting)

struct __FILE
{
	int handle;
};


FILE __stdout;


void _sys_exit(int x)
{
	x  = x;
}


int fputc(int ch, FILE *f)
{
	while( (USART3->SR & 0x40) == 0);
	USART3->DR  = (uint8_t)ch;
	return ch;
}


void _ttywrch(int ch)
{
	ch  = ch;
}
#endif
 
 
 
void initUsart1(uint32_t baudRate)
{
	GPIO_InitTypeDef 		gpioConfig;
	USART_InitTypeDef 		usartConfig;
//	NVIC_InitTypeDef        nvicConfig;
	

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
	
	USART_DeInit(USART1);
	gpioConfig.GPIO_Speed	= GPIO_Speed_50MHz;
	
	gpioConfig.GPIO_Mode	= GPIO_Mode_AF_PP;
	gpioConfig.GPIO_Pin		= USART1_Tx_Pin;
	GPIO_Init(USART1_Tx_Port, &gpioConfig);
	
	gpioConfig.GPIO_Mode	= GPIO_Mode_IN_FLOATING;
	gpioConfig.GPIO_Pin		= USART1_Rx_Pin;
	GPIO_Init(USART1_Rx_Port, &gpioConfig);
	
//	nvicConfig.NVIC_IRQChannel		              = USART1_IRQn;
//	nvicConfig.NVIC_IRQChannelPreemptionPriority  = USART1_Int_Priority_Pre;
//	nvicConfig.NVIC_IRQChannelSubPriority 		  = USART1_Int_Priority_Sub;
//	nvicConfig.NVIC_IRQChannelCmd                 = ENABLE;
//	NVIC_Init(&nvicConfig);
	
	usartConfig.USART_BaudRate				= baudRate;
	usartConfig.USART_HardwareFlowControl   = USART_HardwareFlowControl_None;
	usartConfig.USART_Mode                  = USART_Mode_Tx;
	usartConfig.USART_Parity                = USART_Parity_No;
	usartConfig.USART_StopBits              = USART_StopBits_1;
	usartConfig.USART_WordLength            = USART_WordLength_8b;
	
	USART_Init(USART1, &usartConfig);
	USART_Cmd(USART1, ENABLE);
}

/** USART3 Configuration
 *
 *  Function: 
 *
 *  Tx  PB.10
 *  Rx  PB.11
 */
void initUsart3(uint32_t baudRate)
{
	GPIO_InitTypeDef  GPIOConfig;
	USART_InitTypeDef USARTConfig;
	NVIC_InitTypeDef  NVICConfig;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

	
	GPIOConfig.GPIO_Mode  = GPIO_Mode_AF_PP;
	GPIOConfig.GPIO_Pin   = GPIO_Pin_10;
	GPIOConfig.GPIO_Speed  = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIOConfig);
	
	GPIOConfig.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
	GPIOConfig.GPIO_Pin   = GPIO_Pin_11;
	GPIO_Init(GPIOB, &GPIOConfig);
	
	
	USARTConfig.USART_BaudRate                          = baudRate;
	USARTConfig.USART_HardwareFlowControl               = USART_HardwareFlowControl_None;
	USARTConfig.USART_Mode                              = USART_Mode_Tx;
	USARTConfig.USART_Parity                            = USART_Parity_No;
	USARTConfig.USART_StopBits                          = USART_StopBits_1;
	USARTConfig.USART_WordLength                        = USART_WordLength_8b;
  USART_Init(USART3, &USARTConfig);
	
	
	NVICConfig.NVIC_IRQChannel     = USART3_IRQn;
	NVICConfig.NVIC_IRQChannelPreemptionPriority       = 1;
	NVICConfig.NVIC_IRQChannelSubPriority = 0;
	NVICConfig.NVIC_IRQChannelCmd  = ENABLE;
	NVIC_Init(&NVICConfig);	
	
	USART_Cmd(USART3, ENABLE);
}
